package com.sgb.kjwl.viewmodel.machine;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.PublishLeaseDataEntity;
import com.sgb.kjwl.model.entity.lease.AskLeaseItemEntity;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.UnitEntity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.PickSelectEntity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AskLeaseHouseModel2 extends ViewModel {
    public MutableLiveData<Integer> selectFragLiveData = new MutableLiveData<>();
    public MutableLiveData<String> msgLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> showLoad = new MutableLiveData<>();
    public MutableLiveData<PublishLeaseDataEntity> showEditLiveData = new MutableLiveData<>();

    public MutableLiveData<String> publishLiveData = new MutableLiveData<>();
    public MutableLiveData<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>> showTypeViewLiveData = new MutableLiveData<>();
    public MutableLiveData<List<DictEntity3>> getAllPayWayLiveData = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> initAddressDataLiveData = new MutableLiveData<>();

    public PublishLeaseDataEntity entity = new PublishLeaseDataEntity();//发布数据




    public void getDeviceDetail(String id, String type) {
        showLoad.postValue(true);
        APIManager.getInstance().getMachineAPI().getDeviceDetail(id, type).enqueue(new Callback<BaseEntity<PublishLeaseDataEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<PublishLeaseDataEntity>> call, Response<BaseEntity<PublishLeaseDataEntity>> response) {
                showLoad.postValue(false);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    showEditLiveData.postValue(response.body().getData());
                } else {
                    msgLiveData.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<PublishLeaseDataEntity>> call, Throwable t) {
                showLoad.postValue(false);
            }
        });
    }


    //  发布求租信息
    public void getequimentLeasing(PublishLeaseDataEntity publishleasedataentity) {
        showLoad.postValue(true);
        APIManager.getInstance().getMachineAPI().getequimentLeasing(publishleasedataentity).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                showLoad.postValue(false);
                if (response.body() == null || response.body().getState() == null) {
                    msgLiveData.postValue("发布失败..");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    //保存发布成功后返回的id,用于查看详情
                    if (response.body().getData() != null && response.body().getData() instanceof String) {
                        publishLiveData.postValue((String) response.body().getData());
                    }else{
                        msgLiveData.postValue("发布失败");
                    }
                } else {
                    msgLiveData.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                showLoad.postValue(false);
                msgLiveData.postValue("发布失败..");
            }
        });
    }

    //获取类型分类数据
    public void getTypeModel() {
        APIManager.getInstance().getMachineAPI().getAllTypeList().enqueue(new Callback<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>> call, Response<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        showTypeViewLiveData.postValue(response.body().getData());
                    } else {
                        msgLiveData.postValue("暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>> call, Throwable t) {
                msgLiveData.postValue(t.getMessage());
            }
        });
    }

    // 获取付款方式
    public void getAllPayWay() {
        APIManager.getInstance().getMachineAPI().getAllPayWay().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        getAllPayWayLiveData.postValue(response.body().getData());
                    } else {
                        msgLiveData.postValue("暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {

            }
        });
    }

    //获取省市区
    public void getAddressModel() {
        showLoad.postValue(true);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                showLoad.postValue(false);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    initAddressDataLiveData.postValue(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                showLoad.postValue(false);
                msgLiveData.postValue(t.getMessage());
            }
        });
    }



}
